
* code kids outcomes for mothers born between 1980-82
global path = "/Users/jhainmueller/Box Sync/JHMedBox/"
clear all
set more off 


* load kids from the mothers born 1980 to 1982
* data is at the level of a claim

cd  "$path/daca_oregon_notshared/data/"
use allkids2.dta, clear

* keep essential vars
keep CDE_DIAG_* code3* daten SAK_RECIP DTE_BIRTH NUM_RECIP_AGE CDE_POS CDE_SEX CDE_RACE CDE_ETHNIC daten quarter year mother_SAK_RECIP mother_dob mother_yob mother_mob mother_CWCX 

* label vars
label var daten   "date of claim"
label var quarter "quarter of claim"
label var year    "year of claim"
label var SAK_RECIP "kid ID"
label var mother_CWCX "mom emergency medicaid"
label var mother_SAK_RECIP "mom ID"
label var mother_dob "mom date of birth"
label var mother_yob "mother year of birth"
label var mother_mob "mother year of birth"


* drop quarter 2015 4 because codes switched to ICD 10 codes
drop if year==2015 & quarter==4


// covariates

* kids date of birth
gen    dob = date(DTE_BIRTH,"YMD")
format dob %td
label var dob "date of birth"

* year of birth
gen    yob = year(dob)
label var yob "Year of Birth"

* month of birth
gen       mob = month(dob)
label var mob "Month of Birth (1-12)"

* ethnicity	
gen xxethnhis = CDE_ETHNIC ==0
gen xxethhis  = CDE_ETHNIC == 1
gen xxethdk   = CDE_ETHNIC != 1 & CDE_ETHNIC != 0 

label var xxethnhis "Ethnicity: Not hispanic"
label var xxethhis  "Ethnicity: Hispanic"
label var xxethdk   "Ethnicity: Unkown"

drop CDE_ETHNIC

* race
gen racecat1 = CDE_RACE=="W"
label var racecat1 "Race: White"

gen racecat2 = CDE_RACE=="A"
label var racecat2 "Race: Asian"

gen racecat3 = CDE_RACE=="B"
label var racecat3 "Race: Black"

gen racecat4 = CDE_RACE=="H"
label var racecat4 "Race: Hispanic"

gen racecat5 = CDE_RACE=="I"
label var racecat5 "Race: American Indian"

gen racecat6 = racecat1==0 & racecat2==0 & racecat3==0 & racecat4==0 & racecat5==0
label var racecat6 "Race: Other"

drop CDE_RACE

* gender
gen female = CDE_SEX=="F"
label var female "Female"
drop CDE_SEX

* age in 2015
gen age2015 = 2015-yob
label var age2015 "Age in 2015"

* rescale 
global yvars = "female xxethnhis xxethhis xxethdk racecat1 racecat2 racecat3 racecat4 racecat5 racecat6"
foreach y of global yvars {
replace `y' = `y'*100
}

* tag first obs for each kid
egen    tag1 = tag(SAK_R)


/// outcomes

/*
* note: code* are intermediate variables to aid in the coding of diagnoses
* ICD9 diagnoses codes are in vars CDE_DIAG_1 CDE_DIAG_2 CDE_DIAG_3 CDE_DIAG_4

forvalues i = 1/4 {

gen      code3_`i'        = substr(CDE_DIAG_`i',1,3)
destring code3_`i' , force  gen(code3_`i'n)

}

* so code3_1  for example has the first 3 digits/letters (as string) of the 1st diagnoses 
* so code3_2  for example has the first 3 digits/letters (as string) of the 2nd diagnoses 
* so code3_1n for example has the first 3 digits (as numeric) of the 1st diagnoses if those first 3 are numeric
* etc
*/


/// code mental health outcomes: Post-DACA period 2013-2015

* 300 Anxiety, dissociative and somatoform disorders
* 308 Acute reaction to stress
* 309 Adjustment disorder with depressed mood

* any 300, 308, 309 
gen  temp = (inlist(code3_1n,300,308,309) | ///
             inlist(code3_2n,300,308,309) | ///
             inlist(code3_3n,300,308,309) | ///
             inlist(code3_4n,300,308,309)) & year>=2013
			
egen y_300308309 = max(temp) , by(SAK_RECIP)
drop temp
label var y_300308309 "any 300, 308, 309 (2013-15)"

* any 309 or 309
gen  temp = (inlist(code3_1n,308,309) | ///
             inlist(code3_2n,308,309) | ///
             inlist(code3_3n,308,309) | ///
             inlist(code3_4n,308,309)) & year>=2013
			
egen y_308309 = max(temp) , by(SAK_RECIP)
drop temp
label var y_308309 "any 308 or 309 (2013-15)"

* any 300
gen  temp = (inlist(code3_1n,300) | ///
             inlist(code3_2n,300) | ///
             inlist(code3_3n,300) | ///
             inlist(code3_4n,300)) & year>=2013
			
egen y_300 = max(temp) , by(SAK_RECIP)
drop temp
label var y_300 "any 300 (2013-15)"


/// code mental health outcomes: Pre-DACA period 2003-2012 (including quarters 1 and 2 of 2012)

* define dates to include
gen     ypre = (year<2012)
replace ypre = 1 if year==2012 & (quarter==1 | quarter==2 )

* define kids who are born after Pre-DACA period (excluded for the pre-measures)
gen     qob = quarter(dob)
gen     yexclude = 1 if yob>2012
replace yexclude = 1 if yob==2012 & (qob==3 | qob==4 )

* any 300, 308, 309 
gen  temp = (inlist(code3_1n,300,308,309) | ///
             inlist(code3_2n,300,308,309) | ///
             inlist(code3_3n,300,308,309) | ///
             inlist(code3_4n,300,308,309)) & ypre==1
			
egen     y_300308309_p = max(temp) , by(SAK_RECIP)
replace  y_300308309_p = . if yexclude==1
drop temp
label var y_300308309_p "any 300, 308, 309 (2003-12)"

* any 309 or 309
gen  temp = (inlist(code3_1n,308,309) | ///
             inlist(code3_2n,308,309) | ///
             inlist(code3_3n,308,309) | ///
             inlist(code3_4n,308,309)) & ypre==1
			
egen     y_308309_p = max(temp) , by(SAK_RECIP)
replace  y_308309_p = . if yexclude==1
drop temp
label var y_308309_p "any 308 or 309 (2003-12)"

* any 300
gen  temp = (inlist(code3_1n,300) | ///
             inlist(code3_2n,300) | ///
             inlist(code3_3n,300) | ///
             inlist(code3_4n,300)) & ypre==1
			
egen     y_300_p = max(temp) , by(SAK_RECIP)
replace  y_300_p = . if yexclude==1
drop temp
label var y_300_p "any 300 (2003-12)"


/// code subcategories for 300, 308, 309 in Post-DACA period
*all codes observed in dataset starting with 300, 308, or 309
gl slist 30000	30001	30002	30009	30011	30019	30020	30021	30023	30029	3003	3004	3007	30081	30082	3009	///
3080 3081 3082 3083 3084 3089 ///
3090	3091	30921	30923	30924	30928	30929	3093 3094	30981	30982	30989	3099

foreach x in $slist { 	
g temp=(CDE_DIAG_1=="`x'"|CDE_DIAG_2=="`x'"|CDE_DIAG_3=="`x'"|CDE_DIAG_4=="`x'") 
bys SAK_RECIP: egen y_any`x' =max(temp & year>2012) 
replace y_any`x'=y_any`x'*100
drop temp
}


loc l la var
`l' y_any30000	"300.00 Anxiety state, unspecified"
`l' y_any30001	"300.01 Panic disorder without agoraphobia"
`l' y_any30002	"300.02 Generalized anxiety disorder"
`l' y_any30009	"300.09 Other anxiety states"
`l' y_any30011	"300.11 Conversion disorder"
`l' y_any30019	"300.19 Other and unspecified factitious illness"
`l' y_any30020	"300.20 Phobia, unspecified"
`l' y_any30021	"300.21 Agoraphobia with panic disorder"
`l' y_any30023	"300.23 Social phobia"
`l' y_any30029	"300.29 Other isolated or specific phobias"
`l' y_any3003	"300.3  \hspace{2mm}Obsessive-compulsive disorders"
`l' y_any3004	"300.4  \hspace{2mm}Dysthymic disorder"
`l' y_any3007	"300.7  \hspace{2mm}Hypochondriasis"
`l' y_any30081	"300.81 Somatization disorder"
`l' y_any30082	"300.82 Undifferentiated somatoform disorder"
`l' y_any3009	"300.9  \hspace{2mm}Unspecified nonpsychotic mental disorder"
`l' y_any3080	"308.0  \hspace{2mm}Predominant disturbance of emotions"
`l' y_any3081	"308.1  \hspace{2mm}Predominant disturbance of consciousness"
`l' y_any3082	"308.2  \hspace{2mm}Predominant psychomotor disturbance"
`l' y_any3083	"308.3  \hspace{2mm}Other acute reactions to stress"
`l' y_any3084	"308.4  \hspace{2mm}Mixed disorders as reaction to stress"
`l' y_any3089	"308.9  \hspace{2mm}Unspecified acute reaction to stress"
`l' y_any3090	"309.0  \hspace{2mm}Adjustment disorder, depressed mood"
`l' y_any3091	"309.1  \hspace{2mm}Prolonged depressive reaction"
`l' y_any30921	"309.21 Separation anxiety disorder"
`l' y_any30923	"309.23 Specific academic or work inhibition"
`l' y_any30924	"309.24 Adjustment disorder, anxiety"
`l' y_any30928	"309.28 Adjustment disorder, mixed anxiety/depressed mood"
`l' y_any30929	"309.29 Other adjustment reactions, disturbance of other emotions"
`l' y_any3093	"309.3  \hspace{2mm}Adjustment disorder, disturbance of conduct"
`l' y_any3094	"309.4  \hspace{2mm}Adjustment disorder, mixed disturbance of emotions/conduct"
`l' y_any30981	"309.81 Posttraumatic stress disorder"
`l' y_any30982	"309.82 Adjustment reaction, physical symptoms"
`l' y_any30989	"309.89 Other specified adjustment reactions"
`l' y_any3099	"309.9  \hspace{2mm}Unspecified adjustment reaction"



/// code utilization outcomes:

* visit types (needed to measure HC utilization)

* code outpatient visits
gen     opvisit=.
replace opvisit=1 if CDE_POS=="03"
replace opvisit=1 if CDE_POS=="3"
replace opvisit=1 if CDE_POS=="04"
replace opvisit=1 if CDE_POS=="05"
replace opvisit=1 if CDE_POS=="06"
replace opvisit=1 if CDE_POS=="07"
replace opvisit=1 if CDE_POS=="08"
replace opvisit=1 if CDE_POS=="09"
replace opvisit=1 if CDE_POS=="11"
replace opvisit=1 if CDE_POS=="12"
replace opvisit=1 if CDE_POS=="13"
replace opvisit=1 if CDE_POS=="14"
replace opvisit=1 if CDE_POS=="15"
replace opvisit=1 if CDE_POS=="20"
replace opvisit=1 if CDE_POS=="22"
replace opvisit=1 if CDE_POS=="49"
replace opvisit=1 if CDE_POS=="50"
replace opvisit=1 if CDE_POS=="53"
replace opvisit=1 if CDE_POS=="57"
replace opvisit=1 if CDE_POS=="60"
replace opvisit=1 if CDE_POS=="62"
replace opvisit=1 if CDE_POS=="65"
replace opvisit=1 if CDE_POS=="71"
replace opvisit=1 if CDE_POS=="72"
replace opvisit=1 if CDE_POS=="7"
replace opvisit=1 if CDE_POS=="5"
replace opvisit=1 if CDE_POS=="19"
label var opvisit "outpatient visit"

* code er and urgent care visits
gen     ervisit = .
replace ervisit=1 if CDE_POS=="20"
replace ervisit=1 if CDE_POS=="23"
label var ervisit "ER or urgent care visit"

drop CDE_POS

/// utilization: Post-DACA period 2013-2015

* number of outpatient visits 
egen temp    = nvals(daten)       if opvisit == 1 & year>=2013, by(SAK_RECIP) 
egen y_visitop  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visitop = 0 if y_visitop==.
label var y_visitop "no of op visits (2013-15)"

* number of ER and urgent care visits post 2012
egen temp    = nvals(daten)       if ervisit == 1 & year>=2013, by(SAK_RECIP) 
egen y_visiter  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visiter = 0 if y_visiter==.
label var y_visiter "no of er visits (2013-15)"

* total number of visits post 2012
egen temp    = nvals(daten)       if year>=2013, by(SAK_RECIP) 
egen y_visit  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visit = 0 if y_visit==.
label var y_visit "no of total visits (2013-15)"


/// utilization: Pre-DACA period 2003-2012 (including quarters 1 and 2 of 2012)

* number of outpatient visits 
egen temp    = nvals(daten)       if opvisit == 1 & ypre==1, by(SAK_RECIP) 
egen y_visitop_p  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visitop_p = 0 if y_visitop_p==.
replace   y_visitop_p = . if yexclude==1
label var y_visitop_p "no of op visits (2003-12)"

* number of ER and urgent care visits post 2012
egen temp    = nvals(daten)       if ervisit == 1 & ypre==1, by(SAK_RECIP) 
egen y_visiter_p  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visiter_p = 0 if y_visiter_p==.
replace   y_visiter_p = . if yexclude==1
label var y_visiter_p "no of er visits (2003-12)"

* number of total visits post 2012
egen temp    = nvals(daten)       if ypre==1, by(SAK_RECIP) 
egen y_visit_p  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visit_p = 0 if y_visit_p==.
replace   y_visit_p = . if yexclude==1
label var y_visit_p "no of er visits (2003-12)"


* ***********
* Robustness:

/// Post-DACA period as including quarters 1 and 2 of 2012

/// mental health outcomes

gen     ypost = (year>=2013)
replace ypost = 1 if year==2012 & (quarter==3 | quarter==4 )

* any 300, 308, 309 
gen  temp = (inlist(code3_1n,300,308,309) | ///
             inlist(code3_2n,300,308,309) | ///
             inlist(code3_3n,300,308,309) | ///
             inlist(code3_4n,300,308,309)) & ypost==1
 
egen y_300308309_b = max(temp) , by(SAK_RECIP)
drop temp
label var y_300308309_b "any 300, 308, 309 (2012-15)"

* any 309 or 309
gen  temp = (inlist(code3_1n,308,309) | ///
             inlist(code3_2n,308,309) | ///
             inlist(code3_3n,308,309) | ///
             inlist(code3_4n,308,309)) & ypost==1
			
		
egen y_308309_b = max(temp) , by(SAK_RECIP)
drop temp
label var y_308309_b "any 308 or 309 (2012-15)"

* any 300
gen  temp = (inlist(code3_1n,300) | ///
             inlist(code3_2n,300) | ///
             inlist(code3_3n,300) | ///
             inlist(code3_4n,300)) & ypost==1
	
egen y_300_b = max(temp) , by(SAK_RECIP)
drop temp
label var y_300_b "any 300 (2012-15)"


///  utilization 

egen temp    = nvals(daten)       if opvisit == 1 & ypost==1, by(SAK_RECIP) 
egen y_visitop_b  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visitop_b = 0 if y_visitop_b==.
label var y_visitop_b "no of op visits (2012-15)"

* number of ER and urgent care visits post 2012
egen temp    = nvals(daten)       if ervisit == 1 & ypost==1, by(SAK_RECIP) 
egen y_visiter_b  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visiter_b = 0 if y_visiter_b==.
label var y_visiter_b "no of er visits (2012-15)"

* number of total visits post 2012
egen temp    = nvals(daten)       if ypost==1, by(SAK_RECIP) 
egen y_visit_b  = max(temp), by(SAK_RECIP) 
drop temp
replace   y_visit_b = 0 if y_visit_b==.
label var y_visit_b "no of er visits (2012-15)"


/// code mental health ouctomes with dsm definitions: Post-DACA Period (2013-2015)

* anxiety disorder: 309.21, 300.02, 313.23,300.01,300.29,293.84,300.23,300.09,300.00,300.22
gen  temp = (inlist(CDE_DIAG_1,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_2,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_3,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_4,"30921","30002","31323","30001","30029") | ///
			 inlist(CDE_DIAG_1,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_2,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_3,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_4,"29384","30023","30009","30000","30022")	 ) & year>=2013

egen      y_dsmanx = max(temp) , by(SAK_RECIP)
label var y_dsmanx "anxiety dsm (2013-15)"
drop temp

* adjustment disorder: 309.00,309.3, 309.4,309.24,309.28, 309.9
gen  temp = (inlist(CDE_DIAG_1,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_2,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_3,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_4,"30900","3093","3094","30924","30928","3099")) & year>=2013

egen      y_dsmadj = max(temp) , by(SAK_RECIP)
label var y_dsmadj "adjustment dsm (2013-15)"
drop temp

* adjustment disorder or acute stress disorder: 308.3 and 309.81
gen  temp = (inlist(CDE_DIAG_1,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_2,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_3,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_4,"30900","3093","3094","30924","30928","3099","3083","30981")) & year>=2013

			 egen      y_dsmadjacu = max(temp) , by(SAK_RECIP)
label var y_dsmadjacu "adjustment and acute dsm (2013-15)"
drop temp


/// Pre-DACA period 2003-12

* anxiety disorder: 309.21, 300.02, 313.23,300.01,300.29,293.84,300.23,300.09,300.00,300.22
gen  temp = (inlist(CDE_DIAG_1,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_2,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_3,"30921","30002","31323","30001","30029") | ///
             inlist(CDE_DIAG_4,"30921","30002","31323","30001","30029") | ///
			 inlist(CDE_DIAG_1,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_2,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_3,"29384","30023","30009","30000","30022") | ///
             inlist(CDE_DIAG_4,"29384","30023","30009","30000","30022")	 ) & ypre==1

egen      y_dsmanx_p = max(temp) , by(SAK_RECIP)
replace   y_dsmanx_p = . if yexclude==1
label var y_dsmanx_p "anxiety dsm (2003-12)"
drop temp

* adjustment disorder: 309.00,309.3, 309.4,309.24,309.28, 309.9
gen  temp = (inlist(CDE_DIAG_1,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_2,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_3,"30900","3093","3094","30924","30928","3099") | ///
             inlist(CDE_DIAG_4,"30900","3093","3094","30924","30928","3099")) & ypre==1

egen      y_dsmadj_p = max(temp) , by(SAK_RECIP)
replace   y_dsmadj_p = . if yexclude==1
label var y_dsmadj_p "adjustment dsm (2003-12)"
drop temp

* adjustment disorder or acute stress disorder: 308.3 and 309.81
gen  temp = (inlist(CDE_DIAG_1,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_2,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_3,"30900","3093","3094","30924","30928","3099","3083","30981") | ///
             inlist(CDE_DIAG_4,"30900","3093","3094","30924","30928","3099","3083","30981")) & ypre==1

egen      y_dsmadjacu_p = max(temp) , by(SAK_RECIP)
replace   y_dsmadjacu_p = . if yexclude==1
label var y_dsmadjacu_p "adjustment and acute dsm (2003-12)"
drop temp



/// topcode utilization measures at 99th percentile
foreach y of varlist y_visit* {
sum     `y'           if tag1, det
replace `y' = `r(p99)' if  `y' > `r(p99)' & `y' != .
}


/// rescale outcomes

* rescale outcomes
foreach y of varlist y_300 y_300_b y_300_p y_308309 y_308309_b y_308309_p y_300308309 y_300308309_b y_300308309_p y_dsmadj y_dsmadj_p y_dsmadjacu y_dsmadjacu_p y_dsmanx y_dsmanx_p {
replace `y' = `y'*100
}

/*
* check age of diagnosis 

global path = "~/Box Sync/JHMedBox/"
clear all
set more off 
set matsize 11000

* load kids from the mothers born 1980 to 1982
cd  "$path/daca_oregon_notshared/data/"
use allkids2_recoded.dta, clear

gen yearssold = (daten-dob)/365

gen  temp = (inlist(code3_1n,300,308,309) | ///
             inlist(code3_2n,300,308,309) | ///
             inlist(code3_3n,300,308,309) | ///
             inlist(code3_4n,300,308,309))
			
egen     firstdiag300308309 = min(yearssold) if temp==1, by(SAK_RECIP)
drop temp

gen  temp = (inlist(code3_1n,308,309) | ///
             inlist(code3_2n,308,309) | ///
             inlist(code3_3n,308,309) | ///
             inlist(code3_4n,308,309))
			
egen     firstdiag308309 = min(yearssold) if temp==1, by(SAK_RECIP)
drop temp

gen  temp = (inlist(code3_1n,300) | ///
             inlist(code3_2n,300) | ///
             inlist(code3_3n,300) | ///
             inlist(code3_4n,300))
			
egen     firstdiag300 = min(yearssold) if temp==1, by(SAK_RECIP)
drop temp

sum firstdiag* if tag1
*/

/// RD variables

* code running variable at month level for plots
sort mother_yob mother_mob
egen rv = group(mother_yob mother_mob)

gen rvstring = mother_yob*100 + mother_mob
labmask rv, values(rvstring)

* distance from 6/15/81
gen rvd = mother_dob - 7836
label var rvd "distance of mom dob from cutoff date"

* bandwidth
gen bwd = abs(rvd)
label var bwd "abs(distance of mom dob from cutoff date)"

* treatment var (1 if mother DACA elegible) first eleg dob is 16jun1981
gen Z = rvd>0
label var Z "DACA elegible"

drop ypost ypre yexclude qob CDE_DIAG* code3*

* save recoded data
saveold allkids2_recoded, replace version(11)

